package com.sh.data.engine.infrastructure.config;

import io.minio.MinioClient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/11 11:35
 */
@Slf4j
@Data
@Configuration
@ConditionalOnProperty(value = "data.engine.minio.enable", havingValue = "true")
@ConfigurationProperties(prefix = "data.engine.minio")
public class MinioConfiguration {

    /**
     * Minio endpoint
     */
    private String endpoint = "http://10.88.36.93:9003";

    /**
     * Minio accessKey
     */
    private String accessKey = "minio_hufu_ak";

    /**
     * Minio secretKey
     */
    private String secretKey = "mJcJH4c7iZzhoAnO";

    /**
     * Minio文件存储的bucket名称
     */
    private String fileStorageBucketName = "data-engine";

    /**
     * Minio文件存储的bucket名称
     */
    private String tempFolderParent = "_OCFM6YbSdQror5WT";

    /**
     * 文件访问URL前缀
     */
    private String fileUrlPrefix = "http://localhost:8080/hufu/minio/file";

    /**
     * Minio命令行程序别名
     */
    private String alias = "minio";

    /**
     * Minio命令行程序路径
     */
    private String mc = "/usr/local/share/minio/mc";

    /**
     * 是否启用Minio
     */
    private Boolean enable = true;

    /**
     * Minio demo文件路径
     */
    private String minioDemoFile = "/data/backend/minio-resource/demo.zip";

    /**
     * 启动KES服务的命令
     */
    private String startKesCommand = "/usr/local/share/minio/start_kes.sh";

    /**
     * 启动Minio服务的命令
     */
    private String startMinioCommand = "/usr/local/share/minio/start_minio.sh";

    @Bean
    public MinioClient minioClient() {
        if (StringUtils.isBlank(accessKey)
            || StringUtils.isBlank(secretKey)
            || StringUtils.isBlank(endpoint)) {
            log.warn("missing accessKey or secretKey or endpoint, skip init minio");
            return null;
        }
        return MinioClient.builder().credentials(accessKey, secretKey).endpoint(endpoint).build();
    }
}
